package cs342.uwosh.edu.MyWorldApp;

import cs342.uwosh.edu.R;
import cs342.uwosh.edu.MyWorldApp.cloud.CloudLinker;
import cs342.uwosh.edu.MyWorldApp.misc.AsyncMethod;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CreateNewAccountActivity extends Activity {
	
	
	EditText usernameEditBox;
	EditText passwordEditBox;
	EditText reEnteredPasswordEditBox;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_account);
        usernameEditBox = (EditText)findViewById(R.id.usernameTextBox2);
        passwordEditBox = (EditText)findViewById(R.id.passwordTextBox2);
        reEnteredPasswordEditBox = (EditText)findViewById(R.id.reEnterPasswordTextBox);
	 }
	 
	 public void okButton(View v) {
		 //User inputs from new_account layout
		 String username = usernameEditBox.getText().toString();
		 String password = passwordEditBox.getText().toString();
		 String reEnteredPassword = reEnteredPasswordEditBox.getText().toString();
		 
		 if(username.isEmpty() || password.isEmpty() || reEnteredPassword.isEmpty()){
			 Toast.makeText(getBaseContext(), "Enter some text first", Toast.LENGTH_LONG).show();
		 } else if(!reEnteredPassword.equals(password)) {
			 Toast.makeText(getBaseContext(), "Passwords are not the same", Toast.LENGTH_LONG).show();
		 } else {
			 submitAccountToCloud(username,password);
			 onBackPressed();
		 }
	 }
	 
	 void submitAccountToCloud(String username, String password) {
			CloudLinker l = new CloudLinker(this);
			AsyncMethod<CloudLinker> am = new AsyncMethod<CloudLinker>(CloudLinker.class, l, "doRegister");
			am.execute(username, password);
			String response = (String)am.getResult();
			Toast.makeText(getBaseContext(), response,
					Toast.LENGTH_LONG).show(); // TODO Remove this?
	 }
	 
	 public void cancelButton(View v) {
		 super.onBackPressed();
	 }
}
